#ifdef MBM_IMIBASE_SUPPORT
#include "mbm_mtk_inc.h"

#include "jpeg.h"

#include "mbm_typedefine.h"
#include "mbm_sys_adapter.h"
#include "mbm_memory_adapter.h"
#include "mbm_file_adapter.h"
#include "mbm_png_adapter.h"
#include "mbm_font_adapter.h"




#define MBM_DEBUG_JPEG

#if defined(MBM_DEBUG_JPEG)
#define JPEG_TRACE mbm_trace
#else
#define JPEG_TRACE(...) 
#endif

static void mbm_decode_image_callback(kal_uint8 cause)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    JPEG_TRACE("mbm_decode_image_callback cause=%d", cause);
}

Int32 mbm_jpeg_init(void)
{
	jpeg_decode_process_struct* jpg_decode_handle = (jpeg_decode_process_struct*)MBM_Malloc(sizeof(jpeg_decode_process_struct));

	memset(jpg_decode_handle, 0, sizeof(jpeg_decode_process_struct));

	jpg_decode_handle->image_data_format = IMGDMA_IBW_OUTPUT_RGB888;
	
	jpg_decode_handle->jpeg_decode_cb = mbm_decode_image_callback;
	
	return (Int32)jpg_decode_handle;
}

ImpBool mbm_jpeg_decode(Int32 inst, Int8 *jpeg_data, Int32 jpeg_len, Int8 **rgb24_buf, Int32 *w, Int32 *h)
{
    kal_uint8 dec_ret = JPEG_DECODE_SUCCESS;
	Int32 width = 0, height = 0;
	jpeg_decode_process_struct* jpg_decode_handle = (jpeg_decode_process_struct*)inst;
	
    gdi_image_jpeg_get_dimension((U8*)jpeg_data, jpeg_len, &width, &height);

    JPEG_TRACE("mbm_jpeg_decode width=%d, height=%d", width, height);
    
	jpg_decode_handle->jpeg_file_handle = 0;
	jpg_decode_handle->jpeg_file_buffer_address = (UInt32)jpeg_data;
	jpg_decode_handle->jpeg_file_size = jpeg_len;
	jpg_decode_handle->jpeg_file_buffer_size = jpeg_len;
	jpg_decode_handle->intmem_start_address = (kal_uint32) MBM_INT_Malloc(MAX_IMG_DEC_INT_MEM_SIZE);
	jpg_decode_handle->intmem_size = MAX_IMG_DEC_INT_MEM_SIZE;
	jpg_decode_handle->extmem_start_address = (kal_uint32) MBM_Malloc(MAX_IMG_DEC_EXT_MEM_SIZE);
	jpg_decode_handle->extmem_size = MAX_IMG_DEC_EXT_MEM_SIZE;
	
	jpg_decode_handle->image_buffer_size = width*height*24/8;
    if(jpg_decode_handle->image_buffer_address)
    {
    	MBM_Free(jpg_decode_handle->image_buffer_address);
    }
	jpg_decode_handle->image_buffer_address = (UInt32)MBM_Malloc(jpg_decode_handle->image_buffer_size);
	jpg_decode_handle->image_width = width;
	jpg_decode_handle->image_height = height;

	*rgb24_buf = (Int8*)jpg_decode_handle->image_buffer_address;

	*w = width;
	*h = height;
	
#if defined(__MTK_TARGET__)
#if defined(__SW_JPEG_CODEC_SUPPORT__)
	dec_ret = jpeg_video_play(jpg_decode_handle, KAL_FALSE);
#else /* __SW_JPEG_CODEC_SUPPORT__ */
    /* HW codec */
    dec_ret = jpeg_video_play(
                jpg_decode_handle,
                (kal_uint16*)jpg_decode_handle->image_buffer_address,
                jpg_decode_handle->image_width,
                KAL_FALSE);

#endif /* __SW_JPEG_CODEC_SUPPORT__ */
#endif

    if (jpg_decode_handle->intmem_start_address)
    {
        MBM_INT_Free(jpg_decode_handle->intmem_start_address);
    }
    
    if (jpg_decode_handle->extmem_start_address)
    {
        MBM_Free(jpg_decode_handle->extmem_start_address);
    }

	if (dec_ret != JPEG_DECODE_SUCCESS && dec_ret != JPEG_DECODE_DECODER_OVERFLOW_FAIL)
	{
		return IMP_True;
	}

	return IMP_False;
}

void mbm_jpeg_deinit(Int32 inst)
{
	jpeg_decode_process_struct* jpg_decode_handle = (jpeg_decode_process_struct*)inst;
	
    if(jpg_decode_handle->image_buffer_address)
    {
    	MBM_Free(jpg_decode_handle->image_buffer_address);
    }

    MBM_Free(jpg_decode_handle);
}

ImpBool mbm_png2bmp_file(ImpCStr pngName, ImpCStr bmpName)
{
	Int8* rgb24_buf = NULL;
	Int32 w = 0, h = 0;
	UInt16 wfile_name[FS_MAX_PATH] = {0};
	GDI_RESULT ret = GDI_SUCCEED;

	mbm_png_decode_init();

	mbm_png_decode(pngName, &rgb24_buf, &w, &h);

	memset(wfile_name, 0, sizeof(wfile_name));
	mbm_str_conv_utf8_ucs2(bmpName, strlen(bmpName), wfile_name, sizeof(wfile_name)/2 - 1);
	ret = gdi_image_bmp_encode_file(w, h, 3, (PU8)rgb24_buf, (PS8)wfile_name);
	
	mbm_png_decode_deinit();

	if(GDI_SUCCEED == ret)
	{
		return IMP_True;
	}

	return IMP_False;
}

ImpBool mbm_jpeg2bmp_file(ImpCStr pathname, ImpStr bmpname)
{
	Int32 dec_handle = 0;
	Int8* jpeg_data = NULL;
	Int32 jpeg_len = 0;
	Int8* rgb24_buf = NULL;
	Int32 w = 0, h = 0;
	UInt16 wfile_name[FS_MAX_PATH] = {0};
	Int32 file_handle = 0;
	Int32 readsize = 0;
	GDI_RESULT ret = GDI_SUCCEED;

	mbm_str_conv_utf8_ucs2(pathname, strlen(pathname), wfile_name, sizeof(wfile_name)/2 - 1);
	file_handle = mbm_fs_open(wfile_name, "rb");

	if(0 == file_handle)
	{
		return IMP_False;
	}

	mbm_fs_getfilesize(file_handle, &jpeg_len);

	jpeg_data = MBM_Malloc(jpeg_len);	

	mbm_fs_read(file_handle, jpeg_data, jpeg_len, &readsize);

	mbm_fs_close(file_handle);

	dec_handle = mbm_jpeg_init();

	mbm_jpeg_decode(dec_handle, jpeg_data, jpeg_len, &rgb24_buf, &w, &h);

	memset(wfile_name, 0, sizeof(wfile_name));
	mbm_str_conv_utf8_ucs2(bmpname, strlen(bmpname), wfile_name, sizeof(wfile_name)/2 - 1);
	ret = gdi_image_bmp_encode_file(w, h, 3, (PU8)rgb24_buf, (PS8)wfile_name);
	MBM_Free(jpeg_data);
	
	mbm_jpeg_deinit(dec_handle);

	if(GDI_SUCCEED == ret)
	{
		return IMP_True;
	}

	return IMP_False;
}


#endif/* MBM_IMIBASE_SUPPORT  */

